package com.ala4.oxcafe.repository;

import com.ala4.oxcafe.boot.manager.CustomUserDetails;
import com.ala4.oxcafe.boot.page.PageDomain;
import com.ala4.oxcafe.boot.utils.DetailsHelper;
import com.ala4.oxcafe.common.utils.JSONUtil;
import com.ala4.oxcafe.common.utils.object.ObjectUtil;
import com.ala4.oxcafe.common.utils.text.StrUtil;
import com.ala4.oxcafe.entity.ServiceOrder;
import com.ala4.oxcafe.repository.mapper.ServiceOrderMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.dao.OptimisticLockingFailureException;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 系统附件存储实体
 *
 * @author PING
 * @date 2025-03-25
 */
@Slf4j
@Repository
public class ServiceOrderRepository extends ServiceImpl<ServiceOrderMapper, ServiceOrder> implements IService<ServiceOrder> {

    private LambdaQueryWrapper<ServiceOrder> queryParam(ServiceOrder serviceOrder) {

        LambdaQueryWrapper<ServiceOrder> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.and(StrUtil.isNotEmpty(serviceOrder.getSearchValue()), item -> {
            item.like(ServiceOrder::getCustomerName, serviceOrder.getSearchValue())
                    .or().like(ServiceOrder::getCustomerPhone, serviceOrder.getSearchValue());
        });
        queryWrapper.eq(ObjectUtil.isNotNull(serviceOrder.getServiceNo()), ServiceOrder::getServiceNo, serviceOrder.getServiceNo());
        queryWrapper.eq(ObjectUtil.isNotNull(serviceOrder.getStoreCode()), ServiceOrder::getStoreCode, serviceOrder.getStoreCode());
        queryWrapper.like(ObjectUtil.isNotNull(serviceOrder.getStoreName()), ServiceOrder::getStoreName, serviceOrder.getStoreName());
        queryWrapper.eq(ObjectUtil.isNotNull(serviceOrder.getCustomerName()), ServiceOrder::getCustomerName, serviceOrder.getCustomerName());
        queryWrapper.eq(ObjectUtil.isNotNull(serviceOrder.getCustomerPhone()), ServiceOrder::getCustomerPhone, serviceOrder.getCustomerPhone());
        queryWrapper.eq(ObjectUtil.isNotNull(serviceOrder.getCarPlate()), ServiceOrder::getCarPlate, serviceOrder.getCarPlate());
        queryWrapper.eq(ObjectUtil.isNotNull(serviceOrder.getCarModel()), ServiceOrder::getCarModel, serviceOrder.getCarModel());
        queryWrapper.eq(ObjectUtil.isNotNull(serviceOrder.getCarVin()), ServiceOrder::getCarVin, serviceOrder.getCarVin());
        queryWrapper.eq(ObjectUtil.isNotNull(serviceOrder.getStatus()), ServiceOrder::getStatus, serviceOrder.getStatus());

        CustomUserDetails userDetails = DetailsHelper.getUserDetails();
        // 如果是门店用户 查服务单必须查询门店的
        if (userDetails.getIsStoreUser() == 0) {
            queryWrapper.eq(ServiceOrder::getStoreId, userDetails.getStoreId());
        }

        return queryWrapper;
    }

    public IPage<ServiceOrder> selectServiceOrderPage(PageDomain pageDomain, ServiceOrder serviceOrder) {

        return this.page(pageDomain.toPlusPage(), queryParam(serviceOrder));
    }

    public List<ServiceOrder> selectServiceOrderList(ServiceOrder serviceOrder) {
        return this.list(queryParam(serviceOrder));
    }

    public void updateByIdOptimistic(ServiceOrder serviceOrder) {
        boolean updated = this.updateById(serviceOrder);
        if (!updated) {
            log.error("Optimistic locking failure : {}", JSONUtil.toJsonStr(serviceOrder));
            throw new OptimisticLockingFailureException("更新失败,请刷新重试");
        }
    }
}
